/**
* Copyright (c) 2004-2005 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
 * COMPILE:
 * javac -classpath jenoware.jar JoinTables.java
 *
 * EXECUTE:
 * java -classpath jenoware.jar:postgresql.jar JoinTables -s {host} -d {db name} -u {user} -p {password} table1 table2 ...
 *
 * The program join tables retrieved from the database_name installed into localhost.
 * You need a PostgreSQL database to use the program.
 *
 */

// import jenoware packages
import it.ciano.jenoware.*;
import it.ciano.util.*;

public class JoinTables {
    public JoinTables() {
    }

    public static void main ( String[] args )
    throws java.io.IOException, java.sql.SQLException {
        ReadPar par = myPar ( args );
        par.doMsg();
        if ( !par.checkPar() || par.getParIndex ( "h" ) > -1
        || par.getParIndex ( "s" ) <0 || par.getParIndex ( "d" ) <0
        || par.getParIndex ( "p" ) <0 || par.getParIndex ( "u" ) <0 )
            par.doHelp( );
        else
        {
            // user and password to connect to database
            String host = par.getParValue ( "s" );
            String database = par.getParValue ( "d" );
            String user = par.getParValue ( "u" );
            String password = par.getParValue ( "p" );

            // database connection through a PgSQL object
            PgSQL pg = new PgSQL ( host, database );
            pg.connectPostgreSQL ( user, password );
            // the Pedigree object
            Pedigree ped = new Pedigree();
            // get the list of database tables
            // and select the data from the database tables
            int index = 1;
            while ( index > 0 ) {
                String tt = par.getOptionalAdditionalPar ( index++ );
                if ( tt.equals ( "" ) )
                    index = 0;
                else
                    pg.updatePedigree ( ped, "SELECT * FROM " + tt );
            }

            // print csv to STDOUT using ; as separator
            System.out.print ( ( new GetCSV ( ped, ";", true, true ) ).toString() );

            // closing db connection
            pg.closeDB();
        }
    } // end main

    private final static ReadPar myPar ( String[] input ) {
        ReadPar parameter = new ReadPar ( 100, "2005" );
        parameter.addSintaxHelp ( "JoinTables [options]" );
        parameter.addMessageInfo ( "JoinTables", "0.0.3", "may 4, 2005", "2005",
                                   "Luciano Xumerle <luciano.xumerle@medgen.univr.it>" );
        parameter.addPar ( "h", "Print this help", false );
        parameter.addPar ( "s", "DB host", true );
        parameter.addPar ( "d", "DB name", true );
        parameter.addPar ( "u", "User", true );
        parameter.addPar ( "p", "Password", true );
        parameter.parsePar ( input );
        return parameter;
    }

} //end class

